package plugin.menumarble.open;

import java.io.File;
import java.io.IOException;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.internal.resources.Project;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.ui.handlers.HandlerUtil;

import plugin.menumarble.Agente;
import plugin.menumarble.transformations.NAME_FOLDER;

@SuppressWarnings("restriction")
public class OpenDir implements IHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
		String nameProject="";
		if(selection!=null){
			Object firstElement = selection.getFirstElement();
			if (!(firstElement instanceof Project)){
				MessageDialog.openInformation(HandlerUtil.getActiveShell(event),"Error", "Seleccione un proyecto");
			}
			else{
				File project = new File(firstElement.toString());
				nameProject=project.getName();
				//MessageDialog.openInformation(HandlerUtil.getActiveShell(event),"Proyecto Seleccionado", nameProject);
				String pathdir = "";
				DirectoryDialog dd = new DirectoryDialog(HandlerUtil.getActiveShell(event));
						
				if(dd!=null){
							
					pathdir = dd.open();
					if (pathdir != null && pathdir.length() > 0) {
						System.out.println(pathdir); 
						try {
							Agente.LoadFolder(pathdir, Agente.getPathFolder(NAME_FOLDER.L0, nameProject), nameProject);
						} catch (CoreException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
		
		
	
		return null;
	}

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub
		
	}


}
